package cn.wp.common.core.authentication;

import cn.wp.common.core.authentication.cache.AuthenticationCache;
import cn.wp.common.core.authentication.cache.LocalCache;
import cn.wp.common.core.authentication.cache.RedisCache;
import cn.wp.common.core.authentication.cache.TwoLevelCache;
import cn.wp.common.core.exception.WpException;
import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 认证相关配置
 * Created by chenyb on 2019/09/26
 */
@Configuration
@Slf4j
public class AuthenticationConfiguration {

    @Autowired
    private AuthenticationProperties authenticationProperties;

    /*@Bean
    public Cache caffeineCache(){
        Caffeine<Object,Object> caffeine = Caffeine.newBuilder();
        // fixed 不自动延迟，时间到即自动清除
        if("fixed".equals(authenticationProperties.getTokenTimeoutMode())){
            caffeine.expireAfterWrite(authenticationProperties.getTokenTimeoutSeconds(),TimeUnit.SECONDS);
        }else{
            caffeine.expireAfterAccess(authenticationProperties.getTokenTimeoutSeconds(), TimeUnit.SECONDS);
        }
        // 最大10w个元素，超过会自动清理空间
        caffeine.maximumSize(1024 * 10 * 10);
        caffeine.removalListener(((key, value, cause) -> {
            // 清理通知 key,value ==> 键值对   cause ==> 清理原因
            log.info("caffeine cache remove item (" + key + ":" + JSON.toJSONString(value) + ")");
        }));

        return caffeine.build();
    }*/

    /*@Bean
    public Cache caffeineCache2(){
        Caffeine<Object,Object> caffeine = Caffeine.newBuilder();
        // fixed 不自动延迟，时间到即自动清除
        if("fixed".equals(authenticationProperties.getTokenTimeoutMode())){
            caffeine.expireAfterWrite(authenticationProperties.getTokenTimeoutSeconds(),TimeUnit.SECONDS);
        }else{
            caffeine.expireAfterAccess(authenticationProperties.getTokenTimeoutSeconds(), TimeUnit.SECONDS);
        }
        // 最大10w个元素，超过会自动清理空间
        caffeine.maximumSize(1024 * 10 * 10);
        caffeine.removalListener(((key, value, cause) -> {
            // 清理通知 key,value ==> 键值对   cause ==> 清理原因
            log.info("caffeine cache remove item (" + key + ":" + JSON.toJSONString(value) + ")");
        }));
        return caffeine.build();
    }*/

    @Bean
    public LocalCache localCache(){
        return new LocalCache(authenticationProperties);
    }

    @Bean
    public RedisCache redisCache(){
        return new RedisCache();
    }

    @Bean
    public TwoLevelCache twoLevelCache(){
        return new TwoLevelCache();
    }

    @Bean
    public AuthenticationCache authenticationCache(){
        String type = authenticationProperties.getTokenStorageyType();
        if("local".equals(type)){
            return localCache();
        }

        if("redis".equals(type)){
            return redisCache();
        }

        if("all".equals(type)){
            return twoLevelCache();
        }

        throw new WpException("token-storagey-type setting error. available value: local、redis、all ");
    }

}
